<template>
  <div class="AddOrUpdate_main add_container showDetail noMargin" :class="showKey.type">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <div
          v-for="(item2,index2) in showKey.keys"
          :key="'index2'+index2"
          :class="getClass(item2)"
        >
          <el-form-item v-if="item2.type=='form'" :label="item2.name">
            <div v-if="!showData[item2.key]&&showData[item2.key]!==0&&showData[item2.key]!=='0'" class="shows noData">暂无</div>
            <div v-else-if="item2.key=='status'" class="shows">{{ showData[item2.key]=='1'||showData[item2.key]==1?'启用':'停用' }}</div>
            <div v-else class="shows">{{ showData[item2.key] }}</div>
          </el-form-item>
          <el-form-item v-if="item2.type=='file'" :label="item2.name">
            <FileManageFull
              :file-names="showData[item2.fileName]"
              :file-real-names="showData[item2.fileRealName]"
              :file-urls="showData[item2.fileUrl]"
              :row-index="1"
              :readonly="true"
              @fileName="setFileName"
            />
          </el-form-item>
          <div v-if="item2.type=='table'" class="formItem_header">
            {{ item2.name }}
          </div>
          <div v-if="item2.type=='table'" ref="table_container" class="noPadding table_container draggableTable addRow_table">
            <el-table
              ref="table"
              :data="showData[item2.key]"
              stripe
              border
              row-key="id"
              style="width: 99.9%"
            >
              <el-table-column label="工序" width="180">
                <template slot-scope="scope">
                  <span>{{ scope.row.engineeringProcessName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="外协供应商" width="180">
                <template slot-scope="scope">
                  <span>{{ scope.row.supplierName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="含税单价" min-width="180" prop="price">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.taxPrice" type="number" placeholder="请输入" clearable />
                </template>
              </el-table-column>
              <el-table-column label="税率%" min-width="150" prop="taxRateCode">
                <template slot-scope="scope">
                  <!--                  <span>{{ scope.row.taxRateCode }}</span>-->
                  <el-select
                    v-model="scope.row.taxRateCode"
                    clearable
                    filterable
                    style="width:100%"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in taxRateCodeList"
                      :key="item.dictCode"
                      :label="item.dictLabel"
                      :value="item.dictCode"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column label="查看" min-width="180" prop="taxPrice">
                <template slot-scope="scope">
                  <el-popover
                    :ref="`popover-`+scope.row.id"
                    placement="left"
                    width="700"
                    trigger="click"
                  >
                    <el-button
                      slot="reference"
                      type="text"
                      @click.stop="adjustmentRecord(scope.row)"
                    >调整记录</el-button>
                    <div class="record">
                      <div class="all">
                        <div v-if="JSON.stringify(adjustmentList) !='[]'">
                          <div v-for="item in adjustmentList" :key="item.id" class="oneRecord">
                            <!-- <el-tooltip v-if="tipDisabled" class="item" effect="dark" :content="item.adjustContent" placement="top-start">
                              <span :ref="`domRecord${item.id}`">{{ item.adjustContent }}</span>
                            </el-tooltip> -->
                            <span :ref="`domRecord${item.id}`">{{ item.adjustContent }}</span>
                          </div>

                        </div>
                        <div v-else class="oneRecord">暂无数据</div>
                      </div>
                    </div>
                  </el-popover>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </el-form>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { getDictSelects } from '@/api/common/common'
import { list } from '@/api/purchaseManage/supplierInfo'
import { addOrUpdate, adjust } from '@/api/outsourceManage/outsourcePrice'
import chooseSupplier from './chooseSupplier'
export default {
  name: 'ShowDetailOld',
  props: {
    layerid: {},
    id: {},
    showKey: {},
    isAdd: {},
    showData: {}
  },
  data() {
    return {
      formData: {
      },
      rules: {
      },
      tableHeight: '350',
      taxRateCodeList: [],
      peopleList: [],
      listData: [],
      adjustmentList: [],
      rowId: null,
      tipDisabled: false
    }
  },
  watch: {
    rowId: function(newValue, oldValue) {
      if (oldValue) {
        this.$refs[`popover-` + oldValue][0].doClose()
      }
    }
  },
  mounted() {
    this.formData = this.showData
    this.listData = this.formData.data
  },
  created() {
    this.getDictOptions()
    this.getList()
  },
  methods: {// 表格拖动排序
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.listData.filter(item => !this.SETTINGS.reg_no_zero.test(item.taxPrice)).length > 0) {
            this.$message.warning('含税单价未正确填写')
            return
          }
          this.formData.data = this.listData
          addOrUpdate(this.formData).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$layer.close(this.layerid) // 关闭弹框
            this.$parent.refresh() // 调用父组件的刷新方法
          })
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    },
    mouseEnter(item, index) {
      if (this.$refs[`domRecord${item.id}`][index].offsetWidth > 400) {
        this.tipDisabled = true
      } else {
        this.tipDisabled = false
      }
    },
    adjustmentRecord(row) {
      this.rowId = row.id
      adjust(row.id).then(res => {
        this.adjustmentList = res.data
      })
    },
    getDictOptions() {
      getDictSelects('taxRateCode').then(res => {
        this.taxRateCodeList = res.data
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.peopleList = res.data
      })
    },
    // changePrice(e, row, index) {
    //   if (e.indexOf('.') > -1) {
    //     const afterPoint = e.split('.')
    //     if (e !== 0 && afterPoint[1].length > 2) {
    //       this.listData[index].price = e.slice(0, e.indexOf('.') + 3)
    //       this.$message.warning('至多可输入两位小数')
    //     } else if (e == 0) {
    //       this.listData[index].price = 0
    //     }
    //   } else {
    //     this.listData[index].price = e
    //   }
    //   if (row.taxRateCode) {
    //     this.listData[index].taxPrice = (Number(this.listData[index].price) * (1 + Number(this.listData[index].taxRateLabel) / 100)).toFixed(2)
    //   } else {
    //     this.listData[index].taxPrice = Number(this.listData[index].price).toFixed(2)
    //   }
    // },
    changeTaxPrice(e, row, index) {
      if (row.taxRateCode) {
        this.listData[index].price = (Number(this.listData[index].taxPrice) / (1 + Number(this.listData[index].taxRateLabel) / 100))
      } else {
        this.listData[index].price = Number(this.listData[index].taxPrice)
      }
    },
    chooseSupplier(row, index) {
      this.supplierData = []
      if (this.listData) {
        this.listData.forEach(item => {
          this.supplierData.push(item.supplierId)
        })
      }
      this.rowIndex = index
      this.$layer.iframe({// 弹框
        content: {
          content: chooseSupplier, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row.supplierId ? row.supplierId : '',
            selectionData: this.selectionData
          }// props
        },
        area: ['880px', '700px'],
        title: '选择人员', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    changeTaxRateCode(e, row, index) {
      this.taxRateCodeList.forEach(ele => {
        if (ele.dictCode == e) {
          this.listData[index].taxRateLabel = ele.dictLabel
        }
      })
      if (row.price) {
        this.listData[index].taxPrice = (Number(this.listData[index].price) * (1 + Number(this.listData[index].taxRateLabel) / 100))
      }
    },
    setFileName(...data) {
      this.tableData[data[3]].fileName = data[0]
      this.tableData[data[3]].fileRealName = data[1]
      this.tableData[data[3]].fileUrl = data[2]
    },
    getClass(item) {
      let res = ''
      if (item.type == 'form') {
        res += 'showItem'
        if (item.length && item.length == 'double') {
          res += ' doubles'
        }
      } else if (item.type == 'file') {
        res += 'showItem doubles'
      } else {
        res += 'showTable'
      }

      return res
    }
  }
}
</script>

<style lang="less" scoped>

.main_main{
  .el-form{
    height: 100%;
    align-content: flex-start;

    .showItem{
      margin-left: 20px;
      margin-right: 20px;
      width: calc(33.33% - 40px);
    }

    /deep/ .el-form-item{
      width: 100%;
      border-bottom: 1px solid rgba(105,105,105,0.2);
      margin-left: 0;
      margin-right: 0;

      .el-form-item__label:before{
        content: none;
      }
      .el-form-item__label{
        color: #1F51B3;
        font-size: 15px;
      }
      .el-form-item__content{
        word-break: break-all;
        color: #333333;
        font-size: 15px;
        flex: 1;
        width: unset;
        margin-right: 0px;
      }
    }
    .doubles{
      width: calc(100% - 40px);
      //.show{
      //  overflow: hidden;
      //  text-overflow: ellipsis;
      //  display: -webkit-box;
      //  -webkit-line-clamp: 2;
      //  -webkit-box-orient: vertical;
      //}
    }
  }
  .showTable{
    width: 100%;
  }
}

.column_one{
  .showItem{
    width: 100% !important;
  }
}
.noData{
  color: #c0c4cc;
}
.shows{
  height: fit-content !important;
  padding-top: 10px;
  padding-bottom: 10px;
  color: #c0c4cc;
}

.show{
  height: fit-content !important;
  min-height: 50px;
  padding-top: 10px;
  padding-bottom: 10px;
  color: #c0c4cc;
}
.FileManageFull_container{
  padding-top: 10px;
}

.table_container{
  /deep/ .el-form-item{
    border-bottom: 0 !important;
    width: calc(100% - 40px) !important;
    margin-left: 20px !important;
    margin-right: 20px !important;
    .el-form-item__label:before{
      content: none;
    }
    .el-form-item__content{
      word-break: break-all;
      flex: 1;
      width: unset;
      margin-right: 0px;
    }
  }
}
.record{
  max-height: 180px;
  overflow: auto;
  .all{
    border-left: 1px solid silver;
    padding-left: 10px;
    .oneRecord{
      margin: 10px 0;
      width: 650px;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
  }
}
.noMargin{
  margin-bottom:0 !important;
}
.noPadding{
  padding-top: 0 !important;
}
.formItem_header{
  margin-top: 10px;
}
.el-table {
  font-size: 15px;
}
</style>
